{
  //声明函数
  function createArray(count: number, value: any): any[] {
    //声明一个数组
    let arr: any[] = [];
    //循环
    for (let i = 0; i < count; i++) {
      //将值压入到数组中
      arr.push(value);
    }
    //返回
    return arr;
  }

  //创建一个数组
  let arr = createArray(10, "iloveyou");
  //处理
  // arr[0].slice(0, 5);    //这里没有代码提示
  // arr[0].toFixed(2);     //这里没有报错
}

{
  //声明函数   createArray 是一个泛型函数    T 泛型变量. 名字随意, 一般用大写 常用: T V P
  function createArray<T>(count: number, value: T): T[] {
    //声明一个数组
    let arr: T[] = [];
    //循环
    for (let i = 0; i < count; i++) {
      //将值压入到数组中
      arr.push(value);
    }
    //返回
    return arr;
  }

  let res = createArray<string>(5, "iloveyou");
  // res[0].length     // 这里有提示
  // res[0].toFixed(2);// 这里编译时就报错
}

{
  //多个泛型参数的函数
  function createArray<T, P>(a: T, b: P): [T, P] {
    return [a, b];
  }

  createArray<string, number>("abc", 123)[0].length;
  createArray<string, number>("abc", 123)[1].toFixed(2);
}

{
  //声明对象的结构
  interface Response <T> {
    status: number;
    headers: object;
    data: T;
  }

  interface Book {
    id: number;
    title: string;
    price: number;
  }

  interface User{
    id: number;
    name: string;
    age: number;
  }

  //泛型接口  axios 发送请求返回 promise 成功的结果值
  // axios
  let res: Response<Book> = {
    status: 200,
    headers: {},
    data: {
      id: 1,
      title: "西游记",
      price: 19.9,
    },
  };

  //另一个响应对象
  let res2: Response<User> = {
    status: 201,
    headers: {},
    data: {
      id: 2,
      name: "哲",
      age: 24,
    },
  };
}

{

  interface Response<T>{
    status: number,
    headers: object,
    data: T
  }

  interface ResponseBody<T>{
    code: string,
    msg: string,
    data: T
  }

  let data: ResponseBody<Book> = {
    code: '2000',
    msg: '成功',
    data: {
      id: 1,
      title: '红楼梦',
      price: 20
    }
  }

  interface Book {
    id: number;
    title: string;
    price: number;
  }

  interface User{
    id: number;
    name: string;
    age: number;
  }

  //泛型接口
  let res: Response<ResponseBody<User>>  = {
    status: 200, 
    headers: {},
    data: {
      code: '0000',
      msg: '成功',
      data: {
        id: 1,
        name: '张三',
        age: 18
      }
    }
  }

  let res2: Response<ResponseBody<Book>> = {
    status: 200, 
    headers: {},
    data: {
      code: '0000',
      msg: '成功',
      data: {
        id: 1,
        title: '三国演义',
        price: 19.0
      }
    }
  }

  // console.log(res2);
}

{
  //声明对象的结构  多泛型参数的演示
  interface Response <T, P> {
    status: number;
    headers: P;
    data: T;
  }

  interface Book {
    id: number;
    title: string;
    price: number;
  }

  interface User{
    id: number;
    name: string;
    age: number;
  }

  interface ResponseHeader{
    name: string,
    type: string
  }

  interface ResponseHeader2{
    title: string,
    position: string
  }

  //泛型接口  axios 发送请求返回 promise 成功的结果值
  // axios
  let res: Response<Book, ResponseHeader> = {
    status: 200,
    headers: {
      name: 'test',
      type: 'increment'
    },
    data: {
      id: 1,
      title: "西游记",
      price: 19.9,
    },
  };

  //另一个响应对象
  let res2: Response<User, ResponseHeader2> = {
    status: 201,
    headers: {
      title: 'xxx',
      position: 'xxxx'
    },
    data: {
      id: 2,
      name: "哲",
      age: 24,
    },
  };
}

{
  //创建容器类
  class Container<T>{
    store: T[];
    //构造方法
    constructor(store: T[]){
      this.store = store;
    }
    push(v: T){
      this.store.push(v);
    }
  }

  //实例化对象
  let c = new Container<string>(['xx', 'xx','dd']);
  c.push('ddd');

  console.log(c);
}


{
  interface Lengthwise {
    length: number
  }

  //声明一个函数 获取数据的长度
  function getLength<T extends Lengthwise>(arg: T){
    return arg.length;
  }

  let obj: Lengthwise = {
    length: 10
  }

  console.log(getLength(obj));
}